def prost(n):
   if n == 1:
       return False
   a = 0
   for i in range(2, int(n ** 0.5) + 1):
       if (n % i == 0):
           a = a + 1
   if (a == 0):
       return True
   else:
       return False


def generate(a):
   a = str(a)
   s = []
   for i in range(len(a)):
       for j in range(i, len(a)):
           s.append(a[i:j+1])
   return(s)


d = int(input())
if d <= 999 or d > 9999:
    print(0)
    exit()
guh = generate(d)
guh = [int(x) for x in guh]
guh.sort(reverse=True)
for d in guh:
   if prost(d):
       print(d)
       break
else:
   print(0)